package com.jessechanrui.template.example;

import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * @author chenrui
 * @version 1.0
 * @description HtmlToPdfExample
 * @date 2025/10/9
 */
public class HtmlToPdfExample {
    public static void main(String[] args) {
        // HTML内容 - 使用传统字符串拼接，兼容所有Java版本
        String htmlContent =
                "<!DOCTYPE html>\n" +
                        "<html>\n" +
                        "<head>\n" +
                        "    <meta charset=\"UTF-8\"></meta>\n" +
                        "    <title>OpenHtmlToPdf示例</title>\n" +
                        "    <style>\n" +
                        "        body { font-family: SimSun; }\n" +
                        "        .title { color: #2c3e50; text-align: center; }\n" +
                        "        .content { margin: 20px; }\n" +
                        "        .highlight { color: #e74c3c; font-weight: bold; }\n" +
                        "        table { border-collapse: collapse; width: 100%; margin: 20px 0; }\n" +
                        "        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }\n" +
                        "        th { background-color: #f2f2f2; }\n" +
                        "    </style>\n" +
                        "</head>\n" +
                        "<body>\n" +
                        "    <h1 class=\"title\">使用OpenHtmlToPdf与PDFBox生成PDF</h1>\n" +
                        "    \n" +
                        "    <div class=\"content\">\n" +
                        "        <p>这是一个将HTML转换为PDF的示例。</p>\n" +
                        "        <p>可以支持<span class=\"highlight\">CSS样式</span>、表格等元素：</p>\n" +
                        "        \n" +
                        "        <table>\n" +
                        "            <tr>\n" +
                        "                <th>特性</th>\n" +
                        "                <th>支持情况</th>\n" +
                        "            </tr>\n" +
                        "            <tr>\n" +
                        "                <td>基本HTML标签</td>\n" +
                        "                <td>√ 完全支持</td>\n" +
                        "            </tr>\n" +
                        "            <tr>\n" +
                        "                <td>CSS样式</td>\n" +
                        "                <td>√ 部分支持</td>\n" +
                        "            </tr>\n" +
                        "            <tr>\n" +
                        "                <td>表格</td>\n" +
                        "                <td>√ 支持</td>\n" +
                        "            </tr>\n" +
                        "            <tr>\n" +
                        "                <td>图片</td>\n" +
                        "                <td>√ 支持</td>\n" +
                        "            </tr>\n" +
                        "        </table>\n" +
                        "        \n" +
                        "        <p>图片示例：</p>\n" +
                        "    </div>\n" +
                        "</body>\n" +
                        "</html>";

        // 输出PDF文件路径
        String outputPdfPath = "F:\\html_to_pdf_example.pdf";

        try (OutputStream os = new FileOutputStream(new File(outputPdfPath))) {
            PdfRendererBuilder builder = new PdfRendererBuilder();
            builder.withHtmlContent(htmlContent, null); // 第二个参数为HTML基础路径（无外部资源时设为null）
            builder.toStream(os); // 输出到PDF文件流

            // 不配置任何外部字体，依赖默认字体
            builder.run(); // 执行渲染

            System.out.println("PDF generated successfully! Path: " + new File(outputPdfPath).getAbsolutePath());
        } catch (Exception e) {
            System.err.println("生成PDF时发生错误：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
